#include "device.h"
#include "isr.h"

int16 num = 0;
int16 motor_pwm = 0;
device_t device;

int main()
{
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); // 设置中断优先级分组，2位抢占优先级，2位响应优先级
    
    device.para.timer_frequency = 100;
    device.para.motor_pwm_period = 500;
    device.para.motor_pwm_frequency = 10000;
    device.para.usart_baud = 115200;
    device_init(&device, LED | OLED | KEY | BUZZER | TIMER | MOTOR | MPU6050 | USART);

    while(1) 
	{
        device.led.blink(200, 100);
        //device.usart.send_message('a', time);
        
        uint8 key_value = device.key.scan(200);
        if (key_value == KEY0_ON)   --num;
        if (key_value == KEY1_ON)   ++num;
        if (key_value == KEY2_ON)   device.buzzer.beep(40,20);
        else                        device.buzzer.beep(40,0);
        if (key_value == KEY3_ON)
        {
            motor_pwm++;
            if (motor_pwm >= device.para.motor_pwm_period)
                motor_pwm = 0;
        }
        device.oled.show_str(0, 0, "key_value");    device.oled.show_value_int(70, 0, key_value);
        device.oled.show_str(0, 1, "num");          device.oled.show_value_int(70, 1, num);
        device.oled.show_str(0, 2, "time");         device.oled.show_value_int(70, 2, time);
        device.oled.show_str(0, 3, "motor_pwm");    device.oled.show_value_int(70, 3, motor_pwm);
        if (key_value == (KEY0_ON | KEY3_ON))
        {
            device.oled.show_str(0, 4, "gyr_x");        device.oled.show_value_int(70, 4, device.mpu6050.data->gyro.x);
            device.oled.show_str(0, 5, "gyr_y");        device.oled.show_value_int(70, 5, device.mpu6050.data->gyro.y);
            device.oled.show_str(0, 6, "gyr_z");        device.oled.show_value_int(70, 6, device.mpu6050.data->gyro.z);
        }
        else
        {
            device.oled.show_str(0, 4, "acc_x");        device.oled.show_value_int(70, 4, device.mpu6050.data->acc.x);
            device.oled.show_str(0, 5, "acc_y");        device.oled.show_value_int(70, 5, device.mpu6050.data->acc.y);
            device.oled.show_str(0, 6, "acc_z");        device.oled.show_value_int(70, 6, device.mpu6050.data->acc.z);
        }
        device.oled.show_str(0, 7, "uart_R");           device.oled.show_value_int(70, 7, device.usart.receive_buffer[2]);
        device.motor.output_4ch(motor_pwm, motor_pwm, motor_pwm, motor_pwm);
	}	
}
